package zzu.mxd.utils;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;

import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;

import java.net.URL;

/**
 * Created by zwl on 2018/11/16.
 * May god bless me
 */
@Slf4j
@Component
public class HttpClient {
    @Value("${spring.weChat.appId}")
    private String appId;
    @Value("${spring.weChat.appSecret}")
    private String appSecret;

    public String getOpenId(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String code = request.getParameter("sessionId");
        if (code == null || "".equals(code)) {
            log.warn("sessionId为空");
            return null;
        }
        JSONObject json;
        BufferedReader reader;
        HttpURLConnection urlConnection;
        String url = "https://api.weixin.qq.com/sns/jscode2session";
        String httpUrl = url + "?appid=" + appId + "&secret=" + appSecret + "&js_code=" + code
                + "&grant_type=authorization_code";
        urlConnection = (HttpURLConnection) new URL(httpUrl).openConnection();
        reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "UTF-8"));
        StringBuilder StringBuilder = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            StringBuilder.append(line);
        }
        json = JSONObject.parseObject(StringBuilder.toString());
        String openID;
        openID = json.get("openid").toString();
        if (openID == null || "".equals(openID)) {
            log.warn("openid为空，code为:" + code);
            return null;
        }
        reader.close();

        return openID;
    }

    public static void main(String[] args) throws IOException {
//        String appId = "wx323ea66af0b652da";
//        String appSecret = "de237df084babef20af065756fa11fe6";
//        String code = "001QaBy907fuPw1CfSy901uLy90QaByX";
//        if (code == null || "".equals(code)) {
//            log.warn("sessionId为空");
//
//        }
//        JSONObject json;
//        BufferedReader reader;
//        HttpURLConnection urlConnection;
//        String url = "https://api.weixin.qq.com/sns/jscode2session";
//        String httpUrl = url + "?appid=" + appId + "&secret=" + appSecret + "&js_code=" + code
//                + "&grant_type=authorization_code";
//        urlConnection = (HttpURLConnection) new URL(httpUrl).openConnection();
//        reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "UTF-8"));
//        StringBuilder StringBuilder = new StringBuilder();
//        String line;
//        while ((line = reader.readLine()) != null) {
//            StringBuilder.append(line);
//        }
//        System.out.println(StringBuilder.toString());
//        json = JSONObject.parseObject(StringBuilder.toString());
//        String openID;
//        openID = json.get("openid").toString();
//        if (openID == null || "".equals(openID)) {
//            log.warn("openid为空，code为:" + code);
//
//        }
//        System.out.println(openID);
        String aa="123";
        System.out.println(Long.parseLong(aa));
    }
}
